<!--
* description: 验货通道-新增编辑弹窗
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="400px"
      @close="sendCode"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div class="filter-container filter-params">
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :isMul="comp.isMul"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :options="comp.options"
            :dateOptionsType="comp.dateOptionsType"
            :readonly="comp.readonly"
            :suffixIcon="comp.suffixIcon"
            :showPlaceholder="comp.showPlaceholder"
            :returnCodeField="comp.returnCodeField"
            :returnTextField="comp.returnTextField"
          ></component>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/wms/mixins/formMixins";
import { wmsApis } from "@/api/wms/baseData/api";
import storage from "@/utils/storage";
import request from "@/utils/request";
export default {
  // 组件混入对象
  mixins: [formMixins],
  components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  props: {
    warehouseArr: { type: Array, default: [] }
  },
  watch: {
    popupsVisible(val) {
      if (val) {
        if (
          this.formField.pageId !== "" &&
          this.$route.query.pageId !== undefined &&
          this.$route.query.pageId !== this.formField.pageId
        ) {
          this.formField.pageId = this.$route.query.pageId;
          this.formField.pageName = this.$route.query.pageName;
        }
        this.formField.langLabelName = this.formField.label;
        this.backFormField = JSON.parse(JSON.stringify(this.formField));
      }
    }
  },
  data() {
    return {
      // 保存API配置对象
      apiConfig: wmsApis.saveChannelDelivery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存" //"保存"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "edit_compKey1",
          labelName: "仓库名称", //仓库名称
          codeField: "WAREHOUSE_ID",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 23,
          isMust: true,
          isMul: false,
          options: this.warehouseArr,
          isRequire: true,
          changed: val => {
            this.tableComponents[2].options = [];
            this.formField.CHANNEL_ID = "";
            this.warehouseChange(val);
          },
          disabled: !this.isAdd()
        },
        {
          compKey: "edit_compKey2",
          labelName: "出库去向名称", //出库去向
          textField: "PARTNER_SHORT_NAME",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsOutWhereabouts"),
          type: "propus",
          isMust: true,
          span: 23,
          isMul: false,
          isRequire: true,
          readonly: true,
          returnCodeField: "PARTNER_CODE",
          returnTextField: "PARTNER_SHORT_NAME"
        },
        {
          compKey: "edit_compKey3",
          labelName: "验货通道", //验货通道
          codeField: "CHANNEL_ID",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 23,
          isMust: true,
          isMul: false,
          options: [],
          isRequire: true
        }
      ],
      // 标题
      textMap: {
        edit: "编辑验货通道与出库去向关系",
        add: "新增验货通道与出库去向关系"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        // 仓库
        WAREHOUSE_ID: "",
        // 出库去向
        PARTNER_CODE: "",
        PARTNER_ID: "",
        PARTNER_SHORT_NAME: "",
        // 验货通道
        CHANNEL_ID: "",
        // 键值
        W_PCHANNEL_ID: ""
      }
    };
  },
  methods: {
    onCreated() {
      if (this.popupsVisible) {
        if (this.popupsState === "edit") {
          this.formField.CHANNEL_ID = {
            id: this.dynamicEditRowData.CHANNEL_ID,
            CHANNEL_ID: this.dynamicEditRowData.CHANNEL_ID,
            CHANNEL_CODE: this.dynamicEditRowData.CHANNEL_CODE
          };

          this.backFormField.CHANNEL_ID = this.formField.CHANNEL_ID;
          this.warehouseChange(this.formField.WAREHOUSE_ID);
        }
      }
    },
    reset() {
      this.formField = JSON.parse(JSON.stringify(this.backFormField));
    },
    warehouseChange(code) {
      if (!code) {
        return;
      }
      request({
        url: "/mp/wm/vl/basedata/pickchannel/queryPickChannel.do",
        method: "post",
        data: {
          WAREHOUSE_ID: code,
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (
          res.listResult &&
          res.listResult.rows &&
          res.listResult.rows.length > 0
        ) {
          res.listResult.rows.map(item => {
            this.tableComponents[2].options.push({
              label: item.CHANNEL_NAME,
              value: {
                CHANNEL_ID: item.CHANNEL_ID,
                CHANNEL_CODE: item.CHANNEL_CODE,
                id: item.CHANNEL_ID
              }
            });
          });
        }
      });
    },
    //保存
    save() {
      let PARTNER_CODE = this.formField.PARTNER_CODE;
      let PARTNER_ID = this.formField.PARTNER_ID;
      let { CHANNEL_CODE } = this.formField.CHANNEL_ID;
      let { CHANNEL_ID } = this.formField.CHANNEL_ID;
      let { W_PCHANNEL_ID } = this.formField;
      let reqData = {
        PARTNER_CODE,
        PARTNER_ID,
        CHANNEL_ID,
        CHANNEL_CODE,
        W_PCHANNEL_ID,
        WAREHOUSE_ID: this.formField.WAREHOUSE_ID
      };
      this.$utils.validataMoth(this, "validpopup");
      if (this.valid) {
        var saveObj = JSON.parse(JSON.stringify(reqData));
        //保存表单
        this.saveForm(saveObj);
        storage.remove(this.$route.query.pageCode);
      }
    }
  }
};
</script>
<style type="text/css" scoped>
.filter-params .el-col label .curName {
  width: 150px;
}
</style>